package com.zhenyi.zhengfang.core;

import javax.servlet.http.HttpServletRequest;

import org.hashids.Hashids;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbPro;
import com.jfinal.plugin.activerecord.Page;
import com.utils.PathUtils;
import com.zhenyi.core.ZIndexController;
import com.zhenyi.core.wechatPay.PayUtils;

import cn.hutool.core.util.PageUtil;

public class ZFController extends ZIndexController{
	protected final String APP_ID = PropKit.get("ZF_APP_ID", "");
	protected final String APP_SECRET = PropKit.get("ZF_APP_SECRET", "");
	protected final String PARTNER = PropKit.get("ZF_PARTNER", "");
	protected final String NOTIFY_URL = PropKit.get("ZF_NOTIFY_URL", "");
	protected final String PARTNER_KEY = PropKit.get("ZF_PARTNER_KEY", "");
	protected final String REFUND_NOTIFY_URL = PropKit.get("ZF_REFUND_NOTIFY_URL", "");
	protected final String SSL_PATH = PathUtils.getFile("cert", APP_ID, "apiclient_cert.p12");
	protected final PayUtils payUtils = new PayUtils(APP_ID, PARTNER, PARTNER_KEY, SSL_PATH);

	protected final Hashids HASHID = new Hashids("zhengfang", 8);
	
	//
	protected DbPro db = Db.use("zhengfang");
	protected Integer getPageSize() {
		int pageSize = getParaToInt("pageSize", PAGE_SIZE);
		setAttr("pageSize", pageSize);
		return pageSize;
	}

	protected void setRainbow(Page<?> pages) {
		Integer requestPage = getParaToInt("page");

		HttpServletRequest request = getRequest();
		String url = request.getRequestURL().toString();
		String queryurl = request.getQueryString();
		if (StrKit.notBlank(queryurl)) {
			if (requestPage != null) {
				queryurl = queryurl.replace("&page=" + requestPage, "");
				queryurl = queryurl.replace("page=" + requestPage, "");
				if (StrKit.notBlank(queryurl)) {
					url = url + "?" + queryurl + "&page=";
				} else {
					url = url + "?page=";
				}
			} else {
				url = url + "?" + queryurl + "&page=";
			}
		} else {
			url = url + "?page=";
		}
		int pageTotal = pages.getTotalPage();
		int pageTotalRow = pages.getTotalRow();
		int page = pages.getPageNumber();
		int[] rainbow = PageUtil.rainbow(pages.getPageNumber(), pageTotal);
		setAttr("rainbow", rainbow);
		setAttr("pageUrl", url);
		setAttr("pageTotalRow", pageTotalRow);
		setAttr("pageSize", pages.getPageSize());
		setAttr("pageCurrent", page);
	}

}
